// /**********用户路由(商家)router/user.js*******/
const express = require('express')
const pool = require('../../pool.js')
const router = express.Router()

// /**********正式开始写路由***********/

//1.声明当前路由器下的接口：用户列表
//GET  /showusers
//返回数据：查询到的所有用户
//测试地址：http://127.0.0.1:端口号/user/showusers
router.get('/showusers', (req, res) => {
	let sql = `select id,user,phone,user_name,email,addr,isTrue,roles from user where roles='商家'`
	pool.query(sql, (err, result) => {
		if (err) throw err;
		res.send(result);
	})
})

//2.声明当前路由器下的接口：未审核用户
//GET  /showusers
//条件  isTrue(0:未审核 1:正常 )
//返回数据：查询到的所有未审核商家
//测试地址：http://127.0.0.1:端口号/user/unapproved
router.get('/unapproved', (req, res) => {
	let sql = `select id,user,phone,user_name,email,addr,isTrue,roles from user where isTrue='0'`
	pool.query(sql, (err, result) => {
		if (err) throw err;
		res.send(result);
		//console.log(result)
	})
})

//3.审核用户
//put   /approve
//参数： id
//更新  isTrue=1
router.put('/approve', (req, res) => {
	let sql = `update user set isTrue='1' where id=?`
	let id = req.query.id
	pool.query(sql, [id], (err, result) => {
		if (err) { throw err }
		if (result.affectedRows > 0) {
			res.send({ message: '审核成功！', code: 200 });
		} else {
			res.send({ message: '审核失败！', code: 201 });
		}
	})
})
//4.详情页
//GET  /detail
//参数： id
router.get('/detail', (req, res) => {
	let sql = 'select id,user,phone,user_name,email,addr,roles,isTrue from user where id=?'
	let id = req.query.id
	pool.query(sql, [id], (err, result) => {
		if (err) { throw err }
		res.send(result)
	})
})


//导出路由器
module.exports = router